/*
 *@Author: 张楚
 *@Date: 2021-08-30 14:23:58
 *@Description: dropButton
*/
import 'package:flutter/material.dart';
import 'package:flutter_custom_dropdown/flutter_custom_dropdown.dart';

class DropDownButtonPage extends StatefulWidget {
  const DropDownButtonPage({Key key}) : super(key: key);
  @override
  _DropDownButtonPageState createState() => _DropDownButtonPageState();
}

class _DropDownButtonPageState extends State<DropDownButtonPage> {
  List _fruits = ["全部", "特急", "很急", "平件"];

  List<DropdownMenuItem<String>> _dropDownMenuItems;
  String _selectedFruit;

  @override
  void initState() {
    _dropDownMenuItems = buildAndGetDropDownMenuItems(_fruits);
    // _selectedFruit = _dropDownMenuItems[0].value;
    super.initState();
  }

  List<DropdownMenuItem<String>> buildAndGetDropDownMenuItems(List fruits) {
    List<DropdownMenuItem<String>> items = [];
    for (String fruit in fruits) {
      items.add(DropdownMenuItem(
          value: fruit,
          child: Text(
            fruit,
            style: TextStyle(color: Color(0xff333333), fontSize: 15),
          )));
    }
    return items;
  }

  void changedDropDownItem(String selectedFruit) {
    switch (selectedFruit) {
      case '全部':
        {
          print('全部');
        }

        break;
      case "特急":
        {
          print("特急");
        }

        break;
      case '很急':
        {
          print('很急');
        }

        break;
      case "平件":
        {
          print("平件");
        }

        break;
      default:
    }

    setState(() {
      _selectedFruit = selectedFruit;
    });
    // print(selectedFruit);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("DropDown Button Example"),
      ),
      body: Container(
        child: Center(
            child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("Please choose a fruit: "),
            DropdownButton(
              value: _selectedFruit,
              items: _dropDownMenuItems,
              onChanged: changedDropDownItem,
              hint: new Text('请选择'), //当没有默认值的时候可以设置的提示
              // style: TextStyle(color: Colors.blue),
              // focusColor:Colors.accents ,
              // iconDisabledColor
              iconEnabledColor: Colors.blue,
              iconDisabledColor: Colors.red,
              // onTap: () {
              //   print('object');
              // },
              //背景颜色
              // dropdownColor: Colors.red,
            )
          ],
        )),
      ),
    );
  }
}
